﻿using Dtos;
using IBLL;
using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class StudentsBll : IStudentsBll
    {
        private IStudentsDal _dal;
        public StudentsBll(IStudentsDal dal)
        {
            _dal = dal;
        }
        public async Task<int> AddStudentsAsync(string title)
        {
            return await _dal.AddAsync(new Model.Students { Name = title });
        }

        public async Task<int> DeleteStudentsAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return data != null ? await _dal.DeleteAsync(data) : 0;
        }

        public async Task<List<StudentsDto>> GetAll()
        {
            var data = await _dal.GetAll();
            return data.Select(s => new StudentsDto 
            {
                Id = s.Id,
                Name = s.Name,
                Birthday = s.Birthday,
                Gender = s.Gender,
                Tel = s.Tel,
                Email = s.Email,
                QQNumber = s.QQNumber,
                Address = s.Address,
                GradesId = s.GradesId,
                Pwd = s.Pwd,
                Photo = s.Photo,
            }).ToList();
        }

        public async Task<StudentsDto> GetStudentsByIdAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            if (data == null) 
            {
                return null;
            }
            return new StudentsDto
            {
                Id = data.Id,
                Name = data.Name,
                Birthday = data.Birthday,
                Gender = data.Gender,
                Tel = data.Tel,
                Email = data.Email,
                Pwd = data.Pwd,
                Photo = data.Photo,
                Address = data.Address,
                QQNumber = data.QQNumber,
                GradesId = data.GradesId
            };
        }

        public Task<List<StudentsDto>> GetStudentsByNameAsync(string name)
        {
            throw new NotImplementedException();
        }

        public async Task<bool> IsExistAsync(Guid id, string tel)
        {
            var data = await _dal.GetDataByChoose(u => tel.Equals(u.Tel) && id != u.Id);
            return !data.Any();
        }

        public async Task<StudentsDto> SignlnAsyc(string ac, string pwd)
        {
            var data = await _dal.GetDataByChoose(
                s => (ac.Equals(s.Tel) || ac.Equals(s.Email)) && pwd.Equals(s.Pwd) && s.DeleteId == 1);
            return data.Count > 0 ? new StudentsDto
            {
                Id = data[0].Id,
                Name = data[0].Name,
                Birthday = data[0].Birthday,
                Gender = data[0].Gender,
                Tel = data[0].Tel,
                Email = data[0].Email,
                Pwd = data[0].Pwd,
                Photo = data[0].Photo,
                Address = data[0].Address,
                QQNumber = data[0].QQNumber,
                GradesId = data[0].GradesId
            }:null;
        }

        public async Task<int> UpdatePwdAsync(Guid id, string oldPwd, string newPwd)
        {
            var data = await _dal.GetById(id);
            if(data == null)
                return 0;
            if (data.Pwd.Equals(oldPwd))
            {
                data.Pwd = newPwd;
                data.UpdateTime = DateTime.Now;
                return await _dal.UpdateAsync(data);
            }
            else 
            {
                return -1;
            }
        }

        public async Task<int> UpdateStudentsAsync(Guid id, string name, DateTime birthday, string gender, string tel, string email, string qqNumber, string address, Guid gradesId, string pwd, string photo, DateTime updateTime)
        {
            var data = await _dal.GetById(id);
            if(data == null)
                return 0;
            data.Name = name;
            data.Birthday = birthday;
            data.Gender = gender;
            data.Tel = tel;
            data.Email = email;
            data.QQNumber = qqNumber;
            data.Address = address;
            data.Address = address;
            data.GradesId = gradesId;
            data.Pwd = pwd;
            data.Photo = photo;
            data.UpdateTime = DateTime.Now;
            return await _dal.UpdateAsync(data);
        }
    }
}
